package com.elinkway.infinitemovies.adapter;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.bean.HzPayload;
import com.elinkway.infinitemovies.bean.NewAdDataBean;
import com.elinkway.infinitemovies.bean.PlayRecord;
import com.elinkway.infinitemovies.bean.SDKPayload;
import com.elinkway.infinitemovies.bean.ServerPayload;
import com.elinkway.infinitemovies.bean.TtPayload;
import com.elinkway.infinitemovies.bean.XfPayload;
import com.elinkway.infinitemovies.bean.YzPayload;
import com.elinkway.infinitemovies.config.SettingManage;
import com.elinkway.infinitemovies.dao.DownLoadPlayRecordDao;
import com.elinkway.infinitemovies.download.DownloadEntity;
import com.elinkway.infinitemovies.download.DownloadFolderJob;
import com.elinkway.infinitemovies.download.DownloadHelper;
import com.elinkway.infinitemovies.download.DownloadInfo;
import com.elinkway.infinitemovies.download.DownloadJob;
import com.elinkway.infinitemovies.download.DownloadUtils;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.ui.activity.DownloadActivity;
import com.elinkway.infinitemovies.utils.MoviesUtils;
import com.elinkway.infinitemovies.utils.StringUtil;
import com.elinkway.infinitemovies.utils.Utils;
import com.elinkway.infinitemovies.view.BaiduNativeAdView;
import com.elinkway.infinitemovies.view.GdtNativeAdView;
import com.elinkway.infinitemovies.view.HzPayAdView;
import com.elinkway.infinitemovies.view.NoScrollListView;
import com.elinkway.infinitemovies.view.ServerNativeAdView;
import com.elinkway.infinitemovies.view.TtPayAdView;
import com.elinkway.infinitemovies.view.XfPayAdView;
import com.elinkway.infinitemovies.view.YzPayAdView;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.recyclerlayout.refresh.MyMessageEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

public class DownloadFolderAdapter extends SparseArrayAdapter<DownloadFolderJob> {

    public static String TAG = "DownloadFolderAdapter";

    private boolean deleteState;
    public ArrayList<Boolean> mChecked;
    public int deletedNum;
    public DownloadActivity mContext;
    //获取是否允许3g下载的用户选择数据
    private SettingManage mSettingManage;
    private SharedPreferences mSettingSharePreference;
    private Editor mSettingPreferenceEditor;
    private DownLoadPlayRecordDao downloadPlayRecord;
    private NewAdDataBean mDownloadAd;
    private boolean isOnlyAd;
    private BaiduNativeAdView baiduAdView;

    public void setmDownloadAd(NewAdDataBean mDownloadAd) {
        this.mDownloadAd = mDownloadAd;
    }

    public DownloadFolderAdapter(SparseArray<DownloadFolderJob> list,
                                 DownloadActivity context) {
        super(list, context);
        mContext = context;
        downloadPlayRecord = DownLoadPlayRecordDao.getInstance(mContext);
        initChecked();
        mSettingManage = new SettingManage(mContext);
        mSettingSharePreference = mContext.getSharedPreferences(SettingManage.SETTING_RELATIVE_SHAREPREFERENCE, Context.MODE_PRIVATE);
        mSettingPreferenceEditor = mSettingSharePreference.edit();
    }

    private void initChecked() {
        mChecked = new ArrayList<>();
        for (int i = 0; i < MoviesApplication.getInstance().getDownloadManager().getAllDownloads().size(); i++) {
            mChecked.add(false);
        }
    }

    @Override
    public int getCount() {
        if(mList != null && mList.size() > 0) {
            return mList.size();
        } else {
            return 0;
        }
    }

    public void setIsOnlyAd(boolean isOnlyAd) {
        if (mDownloadAd != null) {
            this.isOnlyAd = isOnlyAd;
        }
    }

    public boolean isOnlyAd() {
        return isOnlyAd;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = mContext.getLayoutInflater();
            convertView = inflater.inflate(R.layout.download_row, null);
            holder.nativeADContainerone = convertView.findViewById(R.id.nativeADContainerone);
            holder.nativeADContainertwo = convertView.findViewById(R.id.nativeADContainertwo);
            holder.imgUrlIv = (ImageView) convertView.findViewById(R.id.left_image_iv);
            holder.downloadName = (TextView) convertView.findViewById(R.id.downloadName);
            holder.downloadLength = (TextView) convertView.findViewById(R.id.downloadLength);
            holder.progressText = (TextView) convertView.findViewById(R.id.rowProgress);
            holder.progressBar = (ProgressBar) convertView.findViewById(R.id.ProgressBar);
            holder.rlDownLayout = (RelativeLayout) convertView.findViewById(R.id.videolayout_new);
            holder.btnDelete = (ToggleButton) convertView.findViewById(R.id.btnDelete);
            holder.folderBg = (LinearLayout) convertView.findViewById(R.id.iv_folder);
            holder.totalvideosIv = (ImageView) convertView.findViewById(R.id.totalvideos_iv);
            holder.ifwatch = (ImageView) convertView.findViewById(R.id.ifwatch);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        DownloadFolderJob folderJob = null;
        holder.nativeADContainerone.setVisibility(View.GONE);
        holder.nativeADContainertwo.setVisibility(View.GONE);
        if (isOnlyAd) {
            holder.rlDownLayout.setVisibility(View.GONE);
        } else if (mList != null && mList.valueAt(position) instanceof DownloadFolderJob) {
            folderJob = mList.valueAt(position);
            holder.rlDownLayout.setVisibility(View.VISIBLE);
        } else {
            holder.rlDownLayout.setVisibility(View.VISIBLE);
            return convertView;
        }

        setDwonloadPageAd(holder, position);

        if (isOnlyAd || folderJob == null) {
            return convertView;
        }

        refreshPart(holder, folderJob, position);

        return convertView;
    }

    /**
     * 设置下载的广告展示
     *
     * @param position
     */
    private void setDwonloadPageAd(ViewHolder holder, int position) {
        if (mDownloadAd != null) {
            if (deleteState) {
                holder.nativeADContainerone.setVisibility(View.GONE);
                holder.nativeADContainertwo.setVisibility(View.GONE);
            } else {
                if(TextUtils.isEmpty(mDownloadAd.provider)){
                    return;
                }else{
                    if (isOnlyAd) {
                        setAdData(holder, mDownloadAd);
                    } else if (mList.size() == 1) {
                        setAdData(holder, mDownloadAd);
                    } else if (mList.size() == 2 && position == 1) {
                        setAdData(holder, mDownloadAd);
                    } else if (mList.size() == 3 && position == 2) {
                        setAdData(holder, mDownloadAd);
                    } else if (mList.size() == 4 && position == 3) {
                        setAdData(holder, mDownloadAd);
                    } else if (mList.size() >= 5 && position == 4) {
                        setAdData(holder, mDownloadAd);
                    }
                }
            }
        }
    }

    private void setAdData(ViewHolder holder, final NewAdDataBean downloadAd) {
        if (Utils.GDT_AD.equals(downloadAd.provider)) {
            setGdtAd(holder, (SDKPayload) downloadAd.payload);
        } else if (Utils.BAIDU_AD.equals(downloadAd.provider)) {
            setBaiduAd(holder, (SDKPayload) downloadAd.payload);
        } else if (Utils.SERVER_AD.equals(downloadAd.provider)) {
            setServerAd(holder, (ServerPayload) downloadAd.payload);
        } else if (Utils.YZ_AD.equals(downloadAd.provider)) {
            setYzServerAd(holder, (YzPayload) downloadAd.payload);
        } else if (Utils.HZ_AD.equals(downloadAd.provider)) {
            setHzServerAd(holder, (HzPayload) downloadAd.payload);
        } else if (Utils.XF_AD.equals(downloadAd.provider)) {
            setXfServerAd(holder, (XfPayload) downloadAd.payload);
        } else if (Utils.TT_AD.equals(downloadAd.provider)) {
            setTtAd(holder, (TtPayload) downloadAd.payload);
        }else {
            return;
        }

    }

    private void setTtAd(ViewHolder holder, TtPayload payload) {
        if (null == payload) {
            return;
        }
        holder.nativeADContainerone.setVisibility(View.VISIBLE);
        if (getListView() instanceof NoScrollListView) {
            if (!((NoScrollListView) getListView()).isOnMeasure()) {
                TtPayAdView ttAdView = new TtPayAdView(mContext, holder.nativeADContainerone, "download_one");
                ttAdView.setTtAd(payload.ad);
            }
        }
    }

    private void setHzServerAd(ViewHolder holder, HzPayload payload) {
        if (null == payload) {
            return;
        }
        holder.nativeADContainerone.setVisibility(View.VISIBLE);
        if (getListView() instanceof NoScrollListView) {
            if (!((NoScrollListView) getListView()).isOnMeasure()) {
                HzPayAdView hzAdView = new HzPayAdView(mContext, holder.nativeADContainerone, "download_one");
                hzAdView.setHzAd(payload.ad);
            }
        }
    }

    private void setYzServerAd(ViewHolder holder, YzPayload payload) {
        if (null == payload) {
            return;
        }
        holder.nativeADContainerone.setVisibility(View.VISIBLE);
        if (getListView() instanceof NoScrollListView) {
            if (!((NoScrollListView) getListView()).isOnMeasure()) {
                YzPayAdView yzAdView = new YzPayAdView(mContext, holder.nativeADContainerone, "download_one");
                yzAdView.setYzAd(payload.ad);
            }
        }
    }

    private void setXfServerAd(ViewHolder holder, XfPayload payload) {
        if (null == payload) {
            return;
        }
        holder.nativeADContainerone.setVisibility(View.VISIBLE);
        if (getListView() instanceof NoScrollListView) {
            if (!((NoScrollListView) getListView()).isOnMeasure()) {
                XfPayAdView xfAdView = new XfPayAdView(mContext, holder.nativeADContainerone, "download_one");
                xfAdView.setXfAd(payload.ad);
            }
        }
    }

    private void setServerAd(ViewHolder holder, ServerPayload serverPayload) {
        if (null == serverPayload) {
            return;
        }
        holder.nativeADContainerone.setVisibility(View.VISIBLE);
        if (getListView() instanceof NoScrollListView) {
            if (!((NoScrollListView) getListView()).isOnMeasure()) {
                ServerNativeAdView serverAd = new ServerNativeAdView(mContext, holder.nativeADContainerone, "download_one");
                serverAd.setServerAd(serverPayload, MoviesHttpApi.DOWNLOAD);
            }
        }
    }

    private void setBaiduAd(ViewHolder holder, SDKPayload sdkPayload) {
        if (null == sdkPayload) {
            return;
        }
        holder.nativeADContainerone.setVisibility(View.VISIBLE);
        if (getListView() instanceof NoScrollListView) {
            if (!((NoScrollListView) getListView()).isOnMeasure()) {
                if (baiduAdView != null) {
                    EventBus.getDefault().unregister(baiduAdView);
                }
                baiduAdView = new BaiduNativeAdView(mContext, holder.nativeADContainerone, "download_one");
                baiduAdView.setBaiduAd(sdkPayload.adId);
                EventBus.getDefault().register(baiduAdView);
            }
        }
    }

    private void setGdtAd(ViewHolder holder, SDKPayload sdkPayload) {
        if (null == sdkPayload) {
            return;
        }
        holder.nativeADContainerone.setVisibility(View.VISIBLE);
        if (getListView() instanceof NoScrollListView) {
            if (!((NoScrollListView) getListView()).isOnMeasure()) {
                GdtNativeAdView gdtAd = new GdtNativeAdView(mContext, "one", holder.nativeADContainerone, "download_one");
                gdtAd.loadAD(sdkPayload.adId);
            }
        }
    }


    public void start(DownloadJob job, ViewHolder holder) {
        if (!job.isCurrentPathExist()) {
            sdcardChangeDialog(mContext, job, holder);
        } else {
            if (null != job.getEntity()) {
                if (TextUtils.isEmpty(job.getEntity().getPath()) && !TextUtils.isEmpty(job.getmDestination())) {
                    job.getEntity().setPath(job.getmDestination());
                }
                job.start();
                if (null != holder) {
                    holder.progressText.setText(mContext.getString(R.string.parentheses_inside_txt, "0.0KB/s"));
                }
            }
        }
    }

    //下载未完成的页面显示与处理
    private void downloadJobUnCompleted(ViewHolder holder, DownloadJob job, int position) {
        holder.progressText.setVisibility(View.VISIBLE);
        holder.progressBar.setVisibility(View.VISIBLE);
        holder.progressBar.setMax(100);
        holder.progressBar.setProgress(job.getProgress());
        holder.progressText.setText(mContext.getString(R.string.parentheses_inside_txt, job.getRate()));
        deleteStateHandler(holder, position);
    }

    private void deleteStateHandler(ViewHolder holder, int position) {
        if (deleteState) {
            holder.btnDelete.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pic_localvideo_uncheck));
            if (position >= mChecked.size()) {
                for (int i = mChecked.size() - 1; i < position; i++) {
                    mChecked.add(false);
                }
            }
            if (position < mChecked.size()) {
                if (mChecked.get(position)) {
                    holder.btnDelete.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pic_localvideo_checked));
                } else {
                    holder.btnDelete.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pic_localvideo_uncheck));
                }
            }
            holder.btnDelete.setVisibility(View.VISIBLE);
            holder.totalvideosIv.setVisibility(View.GONE);
        } else {
            holder.btnDelete.setVisibility(View.GONE);
        }
    }

    //下载完成的页面显示与处理
    private void downloadJobCompleted(ViewHolder holder, DownloadEntity entity, int position) {
        holder.progressBar.setVisibility(View.GONE);
        holder.progressText.setVisibility(View.GONE);
        if (DownloadHelper.getDownloadedFileSize(entity, entity.getPath()) == 0) {
            holder.downloadLength.setText("已完成" + "/" + mContext.getString(R.string.file_has_been_removed));
        } else {
            if (DownloadInfo.M3U8.equals(entity.getDownloadType())) {
                if (entity.getFileSize() < 1024 * 1024) {
                    entity.setFileSize(DownloadHelper.getDownloadedFileSize(entity, entity.getPath()));
                }
                holder.downloadLength.setText(DownloadUtils.getTotalSize(entity.getFileSize()) + "MB");
            } else {
                holder.downloadLength.setText(DownloadUtils.getTotalSize(DownloadHelper.getDownloadedFileSize(entity, entity.getPath())) + "MB");
            }
        }
        deleteStateHandler(holder, position);
    }

    private void showExceptionPause(final ViewHolder holder, final DownloadJob job) {
        if (job.getExceptionType() == DownloadJob.NET_SHUT_DOWN)
            holder.progressText.setText(mContext.getString(R.string.parentheses_inside_txt, mContext.getString(R.string.net_shutdown)));
        if (job.getExceptionType() == DownloadJob.NO_SD)
            holder.progressText.setText(mContext.getString(R.string.parentheses_inside_txt, mContext.getString(R.string.no_sdcard_added)));
        if (job.getExceptionType() == DownloadJob.SD_SPACE_FULL)
            holder.progressText.setText(mContext.getString(R.string.parentheses_inside_txt, mContext.getString(R.string.no_space_tip)));
        if (job.getExceptionType() == DownloadJob.FILE_NOT_FOUND)
            holder.progressText.setText(mContext.getString(R.string.parentheses_inside_txt, mContext.getString(R.string.no_sdcard_added)));
    }

    // 设置下载选中项
    public void setDeleteItem(int position, View view) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        ToggleButton deleteBtn = viewHolder.btnDelete;
        DownloadFolderJob folderJob = mList.valueAt(position);
        int size = folderJob.getDownloadJobs().size();
        if (mChecked.get(position)) {
            deleteBtn.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pic_localvideo_uncheck));
            deletedNum -= size;
            mChecked.set(position, false);
        } else {
            deleteBtn.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pic_localvideo_checked));
            deletedNum += size;
            mChecked.set(position, true);
        }
        if (deletedNum == 0) {
            mContext.restoreDeleteView();
        } else if (deletedNum > 0) {
            showUserSelecedItem();
        }
    }

    public static class ViewHolder {
        public ImageView imgUrlIv;     //视频的图片
        public TextView downloadName; //文件名,如xxxx第几集
        public TextView downloadLength;//大小：15M/88M
        public TextView progressText;//网速/下载中/暂停中/等待中/
        public ProgressBar progressBar;//进度条
        public RelativeLayout rlDownLayout;//整个一个item，用于奇偶变色
        public ToggleButton btnDelete;//删除按钮
        public Button btnPlay;//播放按钮
        public LinearLayout folderBg;
        public ImageView totalvideosIv;
        public ImageView ifwatch;//是否观看过
        public View nativeADContainerone;
        public View nativeADContainertwo;
    }

    public void sdcardChangeDialog(Context context, final DownloadJob dJob, final ViewHolder holder) {
        if (null == context) {
            return;
        }
        String newPath = DownloadHelper.getDownloadPath();
        if (!StringUtil.isEmpty(newPath) && newPath.contains("/" + Utils.getDownLoadFolder())) {
            newPath = newPath.substring(0, newPath.indexOf("/" + Utils.getDownLoadFolder()));
        }
        if (newPath.equals(dJob.getmDestination())) {
            newPath = Utils.SAVE_FILE_PATH_DIRECTORY;
            newPath = newPath.substring(0, newPath.indexOf("/" + Utils.getDownLoadFolder()));
        }
        Builder customBuilder = new Builder(context);
        customBuilder.setTitle(R.string.tip)
                .setMessage(context.getString(R.string.sd_change_message_before) + dJob.getmDestination() + context.getString(R.string.sd_change_message_middle) + newPath + context.getString(R.string.sd_change_message_after))
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        DownloadJob job = dJob;
                        job.setmDestination(DownloadHelper.getDownloadPath());
                        job.getEntity().setPath(DownloadHelper.getDownloadPath());
                        MoviesApplication.getInstance().getDownloadManager().getProvider().updateDownloadEntity(job);
                        start(job, holder);
                        dialog.dismiss();
                    }

                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }

                })
                .setOnKeyListener(new OnKeyListener() {

                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                            dialog.dismiss();
                        }
                        return false;
                    }

                });
        Dialog dialog = customBuilder.create();
        dialog.show();
    }

    public boolean isDeleteState() {
        return deleteState;
    }

    public void setDeleteState(boolean deleteState) {
        this.deleteState = deleteState;
    }

    public void showUserSelecedItem() {
        mContext.deleteView(deletedNum);
    }

    public void refreshPart(ViewHolder holder, DownloadFolderJob folderJob, int position) {
        if (folderJob.getDownloadJobs() == null) {
            return;
        }
        if (folderJob.getDownloadJobs().size() > 1) {
            holder.folderBg.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(folderJob.getDownloadJobs().valueAt(0).getEntity().getImgUrl())) {
                ImageLoader.getInstance().displayImage(folderJob.getDownloadJobs().valueAt(0).getEntity().getImgUrl(), holder.imgUrlIv);
            } else {
                holder.imgUrlIv.setImageResource(R.drawable.ic_launcher);
            }
            holder.downloadName.setText(folderJob.getMediaName());
            holder.downloadLength.setText("已缓存" + folderJob.getDownloadJobs().size() + "集");
            holder.progressBar.setVisibility(View.GONE);
            holder.progressText.setVisibility(View.GONE);
            holder.totalvideosIv.setVisibility(View.VISIBLE);
            deleteStateHandler(holder, position);
            boolean allNotWatch = true;
            for (int i = 0; i < folderJob.getDownloadJobs().size(); i++) {
                DownloadJob job = folderJob.getDownloadJobs().valueAt(i);
                if (job != null) {
                    if ("true".equals(job.getEntity().getIfWatch())) {
                        allNotWatch = false;
                    }
                }
            }
            if (allNotWatch) {
                holder.ifwatch.setVisibility(View.VISIBLE);
            } else {
                holder.ifwatch.setVisibility(View.GONE);
            }
        } else {
            DownloadJob job = folderJob.getDownloadJobs().valueAt(0);
            holder.totalvideosIv.setVisibility(View.GONE);
            holder.folderBg.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(job.getEntity().getImgUrl())) {
                ImageLoader.getInstance().displayImage(job.getEntity().getImgUrl(), holder.imgUrlIv);
            } else {
                holder.imgUrlIv.setImageResource(R.drawable.ic_launcher);
            }
            holder.downloadName.setText(job.getEntity().getDisplayName());
            holder.downloadLength.setText(DownloadUtils.getDownloadedSize(job.getDownloadedSize()) + "MB/" + DownloadUtils.getTotalSize(job.getTotalSize()) + "MB");
            if (job.getProgress() == 100) {
                downloadJobCompleted(holder, job.getEntity(), position);
                if (!"true".equals(job.getEntity().getIfWatch())) {
                    holder.ifwatch.setVisibility(View.VISIBLE);
                } else {
                    holder.ifwatch.setVisibility(View.GONE);
                }
                PlayRecord playRecord = downloadPlayRecord.getByAidAndPorder(job.getEntity().getMid(), job.getEntity().getPorder());
                if (playRecord != null) {
                    holder.progressText.setText(mContext.getString(R.string.vertical_line_record, MoviesUtils.seekHistroyTimeFormat(playRecord.getSeekHistory(), mContext)));
                    holder.progressText.setVisibility(View.VISIBLE);
                } else {
                    holder.progressText.setVisibility(View.GONE);
                }
            } else {
                downloadJobUnCompleted(holder, job, position);
                switch (job.getStatus()) {
                    case DownloadJob.INIT:
                        break;
                    case DownloadJob.DOWNLOADING:
                        break;
                    case DownloadJob.PAUSE:
                    case DownloadJob.NO_USER_PAUSE:
                        holder.progressText.setText(mContext.getString(R.string.parentheses_inside_txt, "暂停中"));
                        showExceptionPause(holder, job);
                        break;
                    case DownloadJob.WAITING:
                        holder.progressText.setText(mContext.getString(R.string.parentheses_inside_txt, "等待中"));
                        if (job.getProgress() == 0)
                            holder.downloadLength.setText("0MB/" + DownloadUtils.getTotalSize(job.getTotalSize()) + "MB");
                        break;
                    default:
                        break;
                }
            }
        }
    }

    public void unregisterBaiduEvent(){
        if(baiduAdView != null){
            EventBus.getDefault().unregister(baiduAdView);
        }
    }

    public void sendBaiduMsg(){
        if(baiduAdView != null){
            EventBus.getDefault().post(new MyMessageEvent(MyMessageEvent.USE_BAIDUCHECK_METHOD));
        }
    }

//    public void refreshView(DownloadFolderJob folderJob, View convertView) {
//        if (folderJob.getDownloadJobs().size() > 1) {
//            convertView.findViewById(R.id.iv_folder).setVisibility(View.VISIBLE);
//            if (!TextUtils.isEmpty(folderJob.getDownloadJobs().valueAt(0).getEntity().getImgUrl())) {
//                ImageLoader.getInstance().displayImage(folderJob.getDownloadJobs().valueAt(0).getEntity().getImgUrl(), (ImageView) convertView.findViewById(R.id.left_image_iv));
//            } else {
//                ((ImageView) convertView.findViewById(R.id.left_image_iv)).setImageResource(R.drawable.ic_launcher);
//            }
//            ((TextView) convertView.findViewById(R.id.downloadName)).setText(folderJob.getMediaName());
//            convertView.findViewById(R.id.totalvideos_iv).setVisibility(View.VISIBLE);
//            ((TextView) convertView.findViewById(R.id.downloadLength)).setText("已缓存" + folderJob.getDownloadJobs().size() + "集");
//            boolean allNotWatch = true;
//            for (int i = 0; i < folderJob.getDownloadJobs().size(); i++) {
//                DownloadJob job = folderJob.getDownloadJobs().valueAt(i);
//                if (job != null) {
//                    if ("true".equals(job.getEntity().getIfWatch())) {
//                        allNotWatch = false;
//                    }
//                }
//            }
//            convertView.findViewById(R.id.rowProgress).setVisibility(View.GONE);
//            if (allNotWatch) {
//                convertView.findViewById(R.id.ifwatch).setVisibility(View.VISIBLE);
//            } else {
//                convertView.findViewById(R.id.ifwatch).setVisibility(View.GONE);
//            }
//        } else {
//            DownloadJob job = folderJob.getDownloadJobs().valueAt(0);
//            convertView.findViewById(R.id.totalvideos_iv).setVisibility(View.GONE);
//            convertView.findViewById(R.id.iv_folder).setVisibility(View.GONE);
//            if (!TextUtils.isEmpty(job.getEntity().getImgUrl())) {
//                ImageLoader.getInstance().displayImage(job.getEntity().getImgUrl(), (ImageView) convertView.findViewById(R.id.left_image_iv));
//            } else {
//                ((ImageView)convertView.findViewById(R.id.left_image_iv)).setImageResource(R.drawable.ic_launcher);
//            }
//            ((TextView) convertView.findViewById(R.id.downloadName)).setText(job.getEntity().getDisplayName());
//            ((TextView) convertView.findViewById(R.id.downloadLength)).setText(DownloadUtils.getDownloadedSize(job.getDownloadedSize()) + "MB/" + DownloadUtils.getTotalSize(job.getTotalSize()) + "MB");
//            if (job.getProgress() == 100) {
//                if (DownloadHelper.getDownloadedFileSize(job.getEntity(), job.getEntity().getPath()) == 0) {
//                    ((TextView) convertView.findViewById(R.id.downloadLength)).setText("已完成" + "/" + mContext.getString(R.string.file_has_been_removed));
//                } else {
//                    if (DownloadInfo.M3U8.equals(job.getEntity().getDownloadType())) {
//                        if (job.getEntity().getFileSize() < 1024 * 1024) {
//                            job.getEntity().setFileSize(DownloadHelper.getDownloadedFileSize(job.getEntity(), job.getEntity().getPath()));
//                        }
//                        ((TextView) convertView.findViewById(R.id.downloadLength)).setText(DownloadUtils.getTotalSize(job.getEntity().getFileSize()) + "MB");
//                    } else {
//                        ((TextView) convertView.findViewById(R.id.downloadLength)).setText(DownloadUtils.getTotalSize(DownloadHelper.getDownloadedFileSize(job.getEntity(), job.getEntity().getPath())) + "MB");
//                    }
//                }
//
//                if (!"true".equals(job.getEntity().getIfWatch())) {
//                    convertView.findViewById(R.id.ifwatch).setVisibility(View.VISIBLE);
//                } else {
//                    convertView.findViewById(R.id.ifwatch).setVisibility(View.GONE);
//                }
//            }
//            PlayRecord playRecord = downloadPlayRecord.getByAidAndPorder(job.getEntity().getMid(), job.getEntity().getPorder());
//            if (playRecord != null) {
//                ((TextView)convertView.findViewById(R.id.rowProgress)).setText(mContext.getString(R.string.vertical_line_record, MoviesUtils.seekHistroyTimeFormat(playRecord.getSeekHistory(), mContext)));
//                convertView.findViewById(R.id.rowProgress).setVisibility(View.VISIBLE);
//            } else {
//                convertView.findViewById(R.id.rowProgress).setVisibility(View.GONE);
//            }
//        }
//    }

}
